<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="ui" uri="http://com.am.tag" %>
<%@ include file="../common.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'userList.jsp' starting page</title>
    <link rel="stylesheet" href="../../css/showtable.css" type="text/css"></link>
    <script type="text/javascript" src="${path}/js/public/grid.js"></script>
    <style type="text/css">
    	.linkActive{color:#ff9900;text-decoration:none;font-weight:bold;}
		.linkNoActive{color:#3399ff;}
    </style>
	<script type="text/javascript">
	
		$().ready(function(){
			addCss("0");
			doPost("0");
			//alert("${currentUserId}");
		});
		
		//0--待审批 1-已审批
		var viewArray = ["0","1"];
		
		function addCss(id){
			/*if(id == "0")
			{
				$("#approve").show();
				$("#history").hide();
			}else{
				$("#approve").hide();
				$("#history").show();
			}*/
			$.each( viewArray, function(n, i){
				if(i == id){
					$("#"+i).find("span").removeClass();
					$("#"+i).find("span").addClass("linkActive");
				}else {
					$("#"+i).find("span").removeClass();
					$("#"+i).find("span").addClass("linkNoActive");
				}
			});
		}
		
		var val;
		function doPost(status){
			val = status;
			addCss(status);
			resetGrid(mygrid,"winForm",{"value":status});
		}
		
		//var value = "${param.value}";
		
		function changeStyle(obj,flag){
			flag ? $(obj).css("background-color","#ffff99") : $(obj).css("background-color","");
		}
		
  		var approve_user_id;
  		var approve_user_name;
  		var notes;
		function doSubmit(){
			if(doCheck() && confirm("确认提交审核吗？")){
				openDailog("view","${path}/jsp/drug/checkSubmitEdit.jsp?",450,140,"盘点提交",false,true,null,function(dlg){
	  				if(dlg.getIFrame().doCheck()){
	  					approve_user_id = dlg.getIFrame().doGetId();
	  					approve_user_name = dlg.getIFrame().doGetName();
	  					notes = dlg.getIFrame().doGetNotes();
		  				doCommit();
		  				dlg.close();
	  				}
	  			});
  			}
  		}
  		
  		var arr = new Array();
  		var i = 0; 
  		function doChoose(){
			/*if(!isSelectOne(mygrid))
			{
				alert("请选择一条记录进行添加");
				return;
			}*/
			var records = mygrid.getSelectedRecords();
			
			for(var k=0;k<records.length;k++){
				var record = records[k];
				if(checkRecord(record)){
					var innerHTML = "<tr onmouseover='changeStyle(this,true)' onmouseout='changeStyle(this,false)' id='"+record['id']+"'>";
					innerHTML += "<td style='display:none;'>"+record['id']+"</td>";
					innerHTML += "<td>"+record['name']+"</td>";
					innerHTML += "<td>"+record['vender']+"</td>";
					innerHTML += "<td align='center'>"+record['spec']+"</td>";
					innerHTML += "<td align='center'>"+record['unit']+"</td>";
					innerHTML += "<td align='center'>"+record['supply_price']+"</td>";
					innerHTML += "<td align='center'>"+record['drug_num']+"</td>";
					innerHTML += "<td><input id='value"+record['id']+"' name='name"+record['id']+"' type='text' class='textbox'></td>";
					innerHTML += "<td align='center'><a href='javascript:void(0)' onclick='delRow("+record['id']+");'>删除</a></td>";
					innerHTML += "</tr>";
					$(".showtable").append(innerHTML);
					arr[i] = record['id'];
					i++;
				}
			}
			mygrid.reload(); //刷新表格
  		}
  		
  		function checkRecord(record){
  			for(var j in arr){
				if(arr[j] == record['id']){
					//alert(record['name'] + "该记录已经存在已选表中");
					return false;
				};
			};
			return true;
  		}
  		
  		function delRow(idIndex){
  			$("tr[id='" + idIndex + "']").remove();//删除当前行
			for(var j in arr){
				if(arr[j] == idIndex){
					arr.splice(j,1);
					return;
				};
			};
  		}
		 
  		function doExport(){
  			$.gridToXls(mygrid,"com.system.provider.DrugListProvider","药品列表.xls","id");
  		}
  		
  		function doCommit(){
   			var map = Form.formToBean("winForm");
       		map.arr = arr;
       		map.value = val;
       		map.approve_user_id = approve_user_id;
       		map.approve_user_name = approve_user_name;
       		map.notes = notes;
       		ajax.remoteCall("com.system.dao.DrugDao:insertResult",[map],function(reply){
       			var result = reply.getResult();
       			alert(result.value);
       			if(result.type == "success")
       			{
       	    		//mygrid.reload(); //刷新表格
       	    		location.reload();//刷新页面
       			}
       		});
    	}
    	
    	function doCheck(){
    		
    		var regExp = /^[0-9]*[1-9][0-9]*$/;
    		
    		/*if(arr.length == 0){
    			alert("没有需要矫正的药品记录");
    			return false;
	    	}*/
	    	
    		for(var j=0;j<arr.length;j++){
    		
    			if(arr[j] != ""){
    		
					var min_num = $.trim($("#value"+arr[j]).val());
					
		    		if(min_num == ""){
		    			alert("盘点数量不能为空");
		    			return false;
		    		}
		    		
		   			if((!regExp.test(min_num))){
		   				if(min_num == '0'){
		   					return true;
		   				}
		   				alert("请输入正整数");
		    			return false;
		   			}
    			}
			};
    		
    		return true;
    	}
  		
  		function doQuery(){
  			resetGrid(mygrid,"queryForm");
  		}
  		
  		function doQueryHistory(){
			openDailog("view","${path}/jsp/drug/checkHistory.jsp?value="+val,800,400,"盘点历史",false,true,null);
  		}
  		
  		
	</script>
  </head>
  
  <body>
  
  
  	<table cellSpacing="0" cellPadding="0" style="width:100%;height:100%;" border="0">
  		<tr>
			<td class="windowTitle">药品管理  &gt; 药品盘点</td>
		</tr>
		
  		<tr id="">
  			<td style="height:35px;width:100%" class="windowHead">	
  				<form id="queryForm" name="queryForm">
					
					<table cellSpacing="0" cellPadding="0" width="100%" height="100%" border="0" >
						<tr>
							<td>
								<img alt="view" src="../../images/menu/view.png" height="18px" width="20px" align="top"></img>
								<font style="font-weight:bold;">&nbsp;视图：</font>&nbsp;&nbsp;
								<a id="0" href="javascript:doPost('0')" ><span>药房盘点</span></a>&nbsp;&nbsp;&nbsp;&nbsp;
								<a id="1" href="javascript:doPost('1')" ><span>药库盘点</span></a>&nbsp;&nbsp;&nbsp;&nbsp;
							</td>
						</tr>
						<tr>
							<td>
								药名：<input name="search_CONTAIN_name" id="name" class="textbox" style="width: 220px" />&nbsp;&nbsp;&nbsp;&nbsp;
								<ui:Button type="query" onclick="doQuery()">查询</ui:Button>&nbsp;&nbsp;&nbsp;&nbsp;
								<ui:Button type="save" onclick="doChoose()">选择</ui:Button>&nbsp;&nbsp;&nbsp;&nbsp;
								<ui:Permission value="invent_emport">
									<ui:Button type="export" onclick="doExport()">导出</ui:Button>&nbsp;&nbsp;&nbsp;&nbsp;
								</ui:Permission>
							</td>
						</tr>
					</table>
				</form>
				<br/>
			</td>
		</tr>
		<tr>
			<td style="width:100%;height:70%;overflow:hidden"  class="windowBody">
				<div id="mess_box" style="width:100%;height:100%;overflow:hidden">
					<ui:Grid id="mygrid" css="width:100%;height:100%"
							 onCellClicked="selectedMultiRow"
				    		 dataProvider="com.system.provider.DrugListProvider:getData" parameters='"low":"2","campus":"${currentUserCampus}"'>
				    	<ui:GridField name="id" header="ID" isChecked="true" align="center"/>
				    	<ui:GridField name="name" header="药品名称" align="left" width="18%"/>
				    	<ui:GridField name="vender" header="生产厂商" align="left" width="19%"/>
				    	<ui:GridField name="spec" header="规格" align="center" width="10%"/>
				    	<ui:GridField name="unit" header="单位" align="center" width="8%"/>
				    	<ui:GridField name="supply_price" header="售价" align="center" width="10%"/>
				    	<ui:GridField name="drug_num" header="库存" align="center" width="9%"/>
				    	<ui:GridField name="total" header="总价" align="center" width="9%"/>
				    	<ui:GridField name="batch_number_valid" header="批号有效期" align="center" width="12%"/>
			    	</ui:Grid>
		    	</div>
		    </td>
		</tr>
		<tr>
			<td class="windowHead" height="35">
				<font size="2"><b>已选药品：</b></font>
				<font color="red" style="font-size: 9.7px">（温馨提示：不重复添加药品！）</font>
			</td>
		</tr>
		<tr>
			<td style="width:100%;height:100%;overflow:hidden"  class="windowBody">
					
				<div id="" style="width:100%;height:100%;overflow:auto">
	  				<form id="winForm" name="winForm">
	  				
					   	<input name="type" type="hidden" value="4">
						<input name="userId" type="hidden" value="${currentUserId}">
						<input name="userName" type="hidden" value="${currentUserName}">
						<input name="userCampus" type="hidden" value="${currentUserCampus}">
						<table cellSpacing="0" cellPadding="2" border="0" class="showtable" width="100%" class="showtable">
							<tr>
								<th style="display:none;">ID</th>
								<th width="20%">药品名称</th>
								<th width="20%">生产厂商</th>
								<th width="10%">规格</th>
								<th width="10%">单位</th>
								<th width="9%">单价</th>
								<th width="9%">库存量</th>
								<th width="5%">盘点数量</th>
								<th width="7%">操作</th>
							</tr>
						</table>
					</form>
				</div>
		    </td>
		</tr>
		<tr id="">
  			<td style="height:35px;width:100%" class="windowHead" colspan="7" align="right">	
  				<ui:Permission value="invent_sumbit">
  					<ui:Button type="save" onclick="doSubmit()">提交</ui:Button>&nbsp;&nbsp;&nbsp;&nbsp;
  				</ui:Permission>
  				<ui:Permission value="invent_history">
  					<ui:Button onclick="doQueryHistory()"><img src="${path}/images/drug/useHistory.png">盘点历史</ui:Button>&nbsp;&nbsp;&nbsp;&nbsp;
  				</ui:Permission>
			</td>
		</tr>
  	</table>
  </body>
</html>
